/* cl.ext.sweetAlert 0.1*/
(
    function (factory) {
        if (typeof define === 'function' && define.amd) {
            define(['jquery',"SweetAlert"], function($) {
                factory($);
            });
        } else if (typeof module === 'object' && module.exports) {
            var sweetAlertjs = require('SweetAlert');
            module.exports = function( root, jQuery ) {
                if ( jQuery === undefined ) {
                    if ( typeof window !== 'undefined' ) {
                        jQuery = require('jquery');
                    }
                    else {
                        jQuery = require('jquery')(root);
                    }
                }
                factory(jQuery);
            };
        } else {
            factory(jQuery);
        }
    }(
    function ($){
        var defaultExtInfo={confirmCallback:null,cancelCallback:null};
        var defaults={infoExt:defaultExtInfo};
        var $modalCommon={
            getDefaults :function () {
                return defaults
            },
            getOptions : function (options) {
                options=$.ExtCommon.isCheckEmpty(options)?{}:options;
                return $.ExtCommon.initMergeJson(this.getDefaults(),options);
            }
        };
        var SweetAlertExtModal = function(options) {};
        SweetAlertExtModal.prototype = {
            close:function(){
                $(".notifyjs-corner").css("z-index","1050");
                swal.close();
            },
            confirm:function(options){
                var self=this;
                var optionBase={
                    type: "warning",
                    showCancelButton:true,
                    confirmButtonColor: "#2196f3",
                    confirmButtonText: "确定",
                    cancelButtonText:"取消",
                    closeOnConfirm:false,
                    showLoaderOnConfirm:true,
                    html: true
                    //allowOutsideClick:true
                };
                var actualOptions=$.ExtCommon.initMergeJson(optionBase,options);
                actualOptions=$modalCommon.getOptions(actualOptions);
                swal(actualOptions, function(isSubmit){
                    if(isSubmit){
                        if(actualOptions.infoExt.confirmCallback){
                            actualOptions.infoExt.confirmCallback(function(success){
                                self.checkClose(success);
                            });
                        }else{
                            self.close();
                        }
                    }else{
                        self.close();
                        if(actualOptions.infoExt.cancelCallback){
                            actualOptions.infoExt.cancelCallback()
                        }
                    }
                });
            },
            checkClose:function(b){
                if(b){
                    this.close();
                }else{
                    $(".notifyjs-corner").css("z-index","10000");
                    $(".sa-button-container").find(":button.cancel,:button.confirm").prop("disabled", false);
                }
            },
            confirmSimple:function(title,text,confirmCallback){
                $.ExtSweetAlert.confirm({title:title, text:text, infoExt:{confirmCallback:confirmCallback}});
            }
        };
        $.ExtSweetAlert = new SweetAlertExtModal;
        $.ExtSweetAlert.Constructor = SweetAlertExtModal;
    })
);
